找传奇、传世资源到传世资源站!

nodejs设计模式第2版.pdf

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈


from clipboardTable of ContentsPreface 1Chapter 1: Welcome to the Node.js Platform 8The Node.js philosophy 8Small core 9Small modules 10Small surface area 11Simplicity and pragmatism 11Introduction to Node.js 6 and ES2015 12The let and const keywords 12The arrow function 15Class syntax 16Enhanced object literals 18Map and Set collections 19WeakMap and WeakSet collections 21Template literals 22Other ES2015 features 23The reactor pattern 24I/O is slow 24Blocking I/O 24Non-blocking I/O 26Event demultiplexing 26Introducing the reactor pattern 29The non-blocking I/O engine of Node.js-libuv 31The recipe for Node.js 31Summary 32Chapter 2: Node.js Essential Patterns 34The callback pattern 35The continuation-passing style 35Synchronous continuation-passing style 35Asynchronous continuation-passing style 36Non-continuation-passing style callbacks 37Synchronous or asynchronous? 38An unpredictable function 38Unleashing Zalgo 39[ ii ]Using synchronous APIs 40Deferred execution 42Node.js callback conventions 43Callbacks come last 43Error comes first 44Propagating errors 44Uncaught exceptions 45The module system and its patterns 47The revealing module pattern 47Node.js modules explained 47A homemade module loader 48Defining a module 50Defining globals 51module.exports versus exports 51The require function is synchronous 51The resolving algorithm 52The module cache 54Circular dependencies 55Module definition patterns 56Named exports 56Exporting a function 57Exporting a constructor 58Exporting an instance 60Modifying other modules or the global scope 61The observer pattern 62The EventEmitter class 63Creating and using EventEmitter 64Propagating errors 65Making any object observable 66Synchronous and asynchronous events 67EventEmitter versus callbacks 68Combining callbacks and EventEmitter 70Summary 71Chapter 3: Asynchronous Control Flow Patterns with Callbacks 72The difficulties of asynchronous programming 73Creating a simple web spider 73The callback hell 76Using plain JavaScript 77Callback discipline 77Applying the callback discipline 78[ iii ]Sequential execution 80Executing a known set of tasks in sequence 81Sequential iteration 82Web spider version 2 82Sequential crawling of links 83The pattern 85Parallel execution 86Web spider version 3 88The pattern 89Fixing race conditions with concurrent tasks 90Limited parallel execution 92Limiting the concurrency 93Globally limiting the concurrency 94Queues to the rescue 94Web spider version 4 95The async library 97Sequential execution 97Sequential execution of a known set of tasks 98Sequential iteration 100Parallel execution 100Limited parallel execution 101Summary 102Chapter 4: Asynchronous Control Flow Patterns with ES2015 andBeyond 104Promise 105What is a promise? 105Promises/A implementations 108Promisifying a Node.js style function 109Sequential execution 111Sequential iteration 112Sequential iteration – the pattern 113Parallel execution 114Limited parallel execution 114Exposing callbacks and promises in public APIs 116Generators 119The basics of generators 119A simple example 120Generators as iterators 121Passing values back to a generator 121Asynchronous control flow with generators 122[ iv ]Generator-based control flow using co 125Sequential execution 125Parallel execution 128Limited parallel execution 130Producer-consumer pattern 131Limiting the download tasks concurrency 133Async await using Babel 134Installing and running Babel 135Comparison 136Summary 138Chapter 5: Coding with Streams 139Discovering the importance of streams 139Buffering versus streaming 139Spatial efficiency 142Gzipping using a buffered API 142Gzipping using streams 143Time efficiency 143Composability 146Getting started with streams 147Anatomy of streams 148Readable streams 149Reading from a stream 149The non-flowing mode 149Flowing mode 150Implementing Readable streams 151Writable streams 153Writing to a stream 153Back-pressure 155Implementing Writable streams 156Duplex streams 158Transform streams 158Implementing Transform streams 159Connecting streams using pipes 162Through and from for working with streams 164Asynchronous control flow with streams 164Sequential execution 164Unordered parallel execution 167Implementing an unordered parallel stream 167Implementing a URL status monitoring application 169Unordered limited parallel execution 170[ v ]Ordered parallel execution 172Piping patterns 173Combining streams 174Implementing a combined stream 175Forking streams 177Implementing a multiple checksum generator 178Merging streams 178Creating a tarball from multiple directories 179Multiplexing and demultiplexing 182Building a remote logger 183Client side – multiplexing 183Server side – demultiplexing 185Running the mux/demux application 187Multiplexing and demultiplexing object streams 187Summary 189Chapter 6: Design Patterns 190Factory 191A generic interface for creating objects 191A mechanism to enforce encapsulation 192Building a simple code profiler 194Composable factory functions 197In the wild 200Revealing constructor 201A read-only event emitter 202In the wild 204Proxy 205Techniques for implementing proxies 206Object composition 206Object augmentation 207A comparison of the different techniques 208Creating a logging Writable stream 208Proxy in the ecosystem – function hooks and AOP 210ES2015 Proxy 210In the wild 212Decorator 213Techniques for implementing Decorators 213Composition 213Object augmentation 214Decorating a LevelUP database 214Introducing LevelUP and LevelDB 215[ vi ]Implementing a LevelUP plugin 215In the wild 217Adapter 218Using LevelUP through the filesystem API 218In the wild 221Strategy 222Multi-format configuration objects 223In the wild 227State 227Implementing a basic fail-safe socket 228Template 233A configuration manager template 234In the wild 236Middleware 236Middleware in Express 236Middleware as a pattern 237Creating a middleware framework for ØMQ 239The Middleware Manager 240A middleware to support JSON messages 242Using the ØMQ middleware framework 243The server 243The client 244Middleware using generators in Koa 245Command 249A flexible pattern 251The task pattern 251A more complex command 251Summary 255Chapter 7: Wiring Modules 256Modules and dependencies 257The most common dependency in Node.js 257Cohesion and coupling 258Stateful modules 259The Singleton pattern in Node.js 259Patterns for wiring modules 261Hardcoded dependency 261Building an authentication server using hardcoded dependencies 262The db module 263The authService module 263The authController module 264[ vii ]The app module 265Running the authentication server 266Pros and cons of hardcoded dependencies 266Dependency Injection 267Refactoring the authentication server to use DI 268The different types of DI 270Pros and cons of DI 271Service locator 273Refactoring the authentication server to use a service locator 274Pros and cons of a service locator 277Dependency Injection container 278Declaring a set of dependencies to a DI container 278Refactoring the authentication server to use a DI container 280Pros and cons of a DI container 283Wiring plugins 283Plugins as packages 283Extension points 286Plugin-controlled vs application-controlled extension 286Implementing a logout plugin 289Using hardcoded dependencies 290Exposing services using a service locator 294Exposing services using DI 296Exposing services using a DI container 298Summary 298Chapter 8: Universal JavaScript for Web Applications 300Sharing code with the browser 301Sharing modules 301Universal Module Definition 302Creating an UMD module 302Considerations on the UMD pattern 305ES2015 modules 305Introducing Webpack 306Exploring the magic of Webpack 307The advantages of using Webpack 308Using ES2015 with Webpack 309Fundamentals of cross-platform development 312Runtime code branching 312Build-time code branching 313Module swapping 316Design patterns for cross-platform development 318[ viii ]Introducing React 319First React component 321JSX, what?! 322Configuring Webpack to transpile JSX 325Rendering in the browser 325The React Router library 327Creating a Universal JavaScript app 332Creating reusable components 332Server-side rendering 335Universal rendering and routing 339Universal data retrieval 340The API server 341Proxying requests for the frontend 343Universal API client 344Asynchronous React components 345The web server 347Summary 350Chapter 9: Advanced Asynchronous Recipes 351Requiring asynchronously initialized modules 351Canonical solutions 352Preinitialization queues 353Implementing a module that initializes asynchronously 353Wrapping the module with preinitialization queues 356In the wild 358Asynchronous batching and caching 359Implementing a server with no caching or batching 359Asynchronous request batching 362Batching requests in the total sales web server 363Asynchronous request caching 365Caching requests in the total sales web server 367Notes about implementing caching mechanisms 368Batching and caching with promises 369Running CPU-bound tasks 371Solving the subset sum problem 372Interleaving with setImmediate 375Interleaving the steps of the subset sum algorithm 375Considerations on the interleaving pattern 377Using multiple processes 378Delegating the subset sum task to other processes 379Implementing a process pool 380[ ix ]Communicating with a child process 382Communicating with the parent process 383Considerations on the multiprocess pattern 385Summary 386Chapter 10: Scalability and Architectural Patterns 387An introduction to application scaling 387Scaling Node.js applications 388The three dimensions of scalability 388Cloning and load balancing 391The cluster module 392Notes on the behavior of the cluster module 393Building a simple HTTP server 393Scaling with the cluster module 395Resiliency and availability with the cluster module 397Zero-downtime restart 399Dealing with stateful communications 401Sharing the state across multiple instances 402Sticky load balancing 403Scaling with a reverse proxy 404Load balancing with Nginx 407Using a service registry 409Implementing a dynamic load balancer with http-proxy and Consul 411Peer-to-peer load balancing 416Implementing an HTTP client that can balance requests across multiple servers 418Decomposing complex applications 419Monolithic architecture 419The microservice architecture 421An example of microservice architecture 421Pros and cons of microservices 423Every service is expendable 424Reusability across platforms and languages 424A way to scale the application 424The challenges of microservices 425Integration patterns in a microservice architecture 425The API proxy 426API orchestration 427Integration with a message broker 431Summary 433Chapter 11: Messaging and Integration Patterns 435Fundamentals of a messaging system 436[ x ]One-way and request/reply patterns 436Message types 438Command Message 439Event Message 439Document Message 439Asynchronous messaging and queues 439Peer-to-peer or broker-based messaging 440Publish/subscribe pattern 442Building a minimalist real-time chat application 443Implementing the server side 444Implementing the client side 445Running and scaling the chat application 446Using Redis as a message broker 447Peer-to-peer publish/subscribe with ØMQ 450Introducing ØMQ 451Designing a peer-to-peer architecture for the chat server 451Using the ØMQ PUB/SUB sockets 452Durable subscribers 454Introducing AMQP 457Durable subscribers with AMQP and RabbitMQ 459Designing a history service for the chat application 460Implementing a reliable history service using AMQP 461Integrating the chat application with AMQP 463Pipelines and task distribution patterns 464The ØMQ fanout/fanin pattern 466PUSH/PULL sockets 466Building a distributed hashsum cracker with ØMQ 467Implementing the ventilator 468Implementing the worker 469Implementing the sink 470Running the application 470Pipelines and competing consumers in AMQP 470Point-to-point communications and competing consumers 471Implementing the hashsum cracker using AMQP 471Implementing the producer 472Implementing the worker 473Implementing the result collector 474Running the application 475Request/reply patterns 475Correlation identifier 475Implementing a request/reply abstraction using correlation identifiers 476Abstracting the request 477[ xi ]Abstracting the reply 478Trying the full request/reply cycle 479Return address 480Implementing the return address pattern in AMQP 481Implementing the request abstraction 482Implementing the reply abstraction 483Implementing the requestor and the replier 484Summary 486Index 487

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复